package com.lopo.util;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;

/*
 * @Author: yangqifang
 * @date: 2022/2/28
 * @description: jackson工具类
 */
@Slf4j
public class JackSonUtils {
    private static ObjectMapper mapper;

    static {
        mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
        mapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }
    public static String toJson(Object object) {
        try {
            return mapper.writeValueAsString(object);
        } catch (IOException var3) {
            log.error("write to json string error:" + object, var3);
            return null;
        }
    }

    public static <T> T fromJson(String jsonString, Class<T> clazz) {
        if (StringUtils.isEmpty(jsonString)) {
            return null;
        } else {
            try {
                return mapper.readValue(jsonString, clazz);
            } catch (IOException var4) {
                log.error("parse json string error:" + jsonString, var4);
                return null;
            }
        }
    }

    public static <T> T fromJson(String jsonString, JavaType javaType) {
        if (StringUtils.isEmpty(jsonString)) {
            return null;
        } else {
            try {
                return mapper.readValue(jsonString, javaType);
            } catch (IOException var4) {
                log.error("parse json string error:" + jsonString, var4);
                return null;
            }
        }
    }

    public static JavaType contructCollectionType(Class<? extends Collection> collectionClass, Class<?> elementClass) {
        return mapper.getTypeFactory().constructCollectionType(collectionClass, elementClass);
    }

    public static JavaType contructMapType(Class<? extends Map> mapClass, Class<?> keyClass, Class<?> valueClass) {
        return mapper.getTypeFactory().constructMapType(mapClass, keyClass, valueClass);
    }

    public static <T> T update(String jsonString, T object) {
        try {
            return mapper.readerForUpdating(object).readValue(jsonString);
        } catch (IOException var4) {
            log.error("update json string:" + jsonString + " to object:" + object + " error.", var4);
        }

        return null;
    }


    public static <T> T fromJson(Object object, Class<T> clazz) {
        if (null == object) {
            return null;
        } else {
            String jsonString = null;

            try {
                if (object instanceof String) {
                    return fromJson(object.toString(), clazz);
                } else {
                    jsonString = toJson(object);
                    return mapper.readValue(jsonString, clazz);
                }
            } catch (IOException var5) {
                log.error("parse json string error:" + jsonString, var5);
                return null;
            }
        }
    }

    public static <T> T fromJson(String jsonString, TypeReference<T> typeReference) {
        if (StringUtils.isEmpty(jsonString)) {
            return null;
        } else {
            try {
                return mapper.readValue(jsonString, typeReference);
            } catch (IOException var4) {
                log.error("parse json string error:" + jsonString, var4);
                return null;
            }
        }
    }
}
